// AI聊天相关类型定义

export interface ChatMessage {
  id: string
  role: 'user' | 'assistant'
  content: string
  reasoning?: string // 推理内容，仅AI消息可能有
  timestamp: Date
  error?: boolean
}

export interface ChatRequest {
  message: string
  model?: string
  // 图片理解相关字段
  isImageUnderstanding?: boolean
  uploadedImage?: string // base64格式的图片
}

export interface ApiKeyConfig {
  apiKey: string
  model: string
}

export interface ApiKeyResponse {
  hasApiKey: boolean
  model?: string
  maskedApiKey?: string
}

export interface TestApiKeyResponse {
  valid: boolean
  message: string
}

export interface ChatSession {
  id: number
  title: string
  model: string
  messageCount: number
  createdAt: string
  updatedAt: string
  lastMessage?: string
  lastMessageRole?: string
  lastMessageTime?: string
}

export interface ChatSessionDetail {
  id: number
  title: string
  model: string
  messageCount: number
  createdAt: string
  updatedAt: string
  messages: ChatMessageDTO[]
}

// 请求类型
export interface CreateSessionRequest {
  title?: string
  model: string
}

export interface UpdateSessionTitleRequest {
  title: string
}

export interface SaveMessageRequest {
  role: 'user' | 'assistant'
  content: string
  tokenCount?: number
}

// 后端返回的消息类型
export interface ChatMessageDTO {
  id: number
  sessionId: number
  role: 'user' | 'assistant'
  content: string
  tokenCount: number
  createdAt: string
}

// 可用的AI模型
export interface AIModel {
  value: string
  label: string
  description?: string
  free?: boolean
}

// 默认可用模型（仅聊天模型）
export const AVAILABLE_MODELS: AIModel[] = [
  {
    value: 'THUDM/GLM-4.1V-9B-Thinking',
    label: 'GLM-4.1V-9B-Thinking',
    description: '智谱AI最新思维模型，支持复杂推理',
    free: false
  },
  {
    value: 'THUDM/GLM-Z1-9B-0414',
    label: 'GLM-Z1-9B-0414',
    description: '智谱AI高效对话模型',
    free: false
  }
]

// SSE事件类型
export interface SSEChunk {
  data: string
  error?: string
}

// 聊天状态
export type ChatStatus = 'idle' | 'loading' | 'streaming' | 'error'

// ==================== 图片生成相关类型 ====================

// 图片尺寸选项
export interface ImageSizeOption {
  value: string
  label: string
  ratio: string
}

// 可用的图片尺寸
export const IMAGE_SIZE_OPTIONS: ImageSizeOption[] = [
  { value: '1024x1024', label: '1024×1024', ratio: '1:1' },
  { value: '960x1280', label: '960×1280', ratio: '3:4' },
  { value: '768x1024', label: '768×1024', ratio: '3:4' },
  { value: '720x1440', label: '720×1440', ratio: '1:2' },
  { value: '720x1280', label: '720×1280', ratio: '9:16' }
]

// 图片生成配置
export interface ImageGenerationConfig {
  imageSize: string
  batchSize: number
  guidanceScale: number
  numInferenceSteps: number
  uploadedImage?: string // base64格式的上传图片
}

// 图片生成请求
export interface ImageGenerationRequest {
  prompt: string
  config: ImageGenerationConfig
  isImageGeneration: true // 标识这是图片生成请求
}

// 图片生成响应
export interface ImageGenerationResponse {
  images: Array<{
    url: string
  }>
  timings: {
    inference: number
  }
  seed: number
  created: number
}

// 扩展ChatMessage支持图片
export interface ImageChatMessage extends Omit<ChatMessage, 'content'> {
  content: string
  images?: string[] // 生成的图片URL数组
  isImageGeneration?: boolean
}

// 扩展ChatRequest支持图片生成
export interface ExtendedChatRequest extends ChatRequest {
  imageGeneration?: ImageGenerationConfig
  isImageGeneration?: boolean
}
